import { copyToClipboard } from "../../../../tools/Tools";
/**
 * 获取划选范围内的行索引和列索引，去重
 * @param ranges
 * @param rowOffset
 * @param columnOffset
 */
export const getRangeRowAndColumns = function (ranges, rowOffset = 0, columnOffset = 0) {
    const Rows = new Set();
    const Columns = new Set();
    if (!ranges || ranges.length < 1) {
        return null;
    }
    for (let i = 0; i < ranges.length; i++) {
        const cellInfo = ranges[i];
        const [rowIndex, columnIndex] = cellInfo["coord"].split("_");
        Rows.add(parseInt(rowIndex) + rowOffset);
        Columns.add(parseInt(columnIndex) + columnOffset);
    }
    return {
        rowIndexs: Array.from(Rows),
        columnIndexs: Array.from(Columns),
    };
};
/**
 * 拷贝划选范围内的值
 * @param range  范围
 * @param type   0: 数值  1: 文本 ， 2: 所有
 */
export const copyRangeValue = function (ranges, type) {
    let copyStr = "";
    let lastRowIndex = "";
    for (let i = 0; i < ranges.length; i++) {
        const cellInfo = ranges[i];
        const dataType = cellInfo["dataType"];
        if (type === 0) {
            if (dataType !== "number") {
                continue;
            }
        }
        if (type === 1) {
            if (dataType !== "string") {
                continue;
            }
        }
        const [rowIndex, columnIndex] = cellInfo["coord"].split("_");
        if (rowIndex !== lastRowIndex && lastRowIndex !== "") {
            copyStr = copyStr.substring(0, copyStr.lastIndexOf("\t"));
            copyStr += "\r\n";
        }
        copyStr += cellInfo["val"] + "\t";
        lastRowIndex = rowIndex;
    }
    copyStr = copyStr.substring(0, copyStr.lastIndexOf("\t"));
    copyToClipboard(copyStr);
};
/**
 * 根据DevExpress原生进行的拷贝逻辑
 * @param selectDatas
 * @param type
 */
export const copyRangeBySelectData = function (selectDatas, showField, type) {
    const copy = [];
    for (let i = 0; i < selectDatas.length; i++) {
        const data = selectDatas[i];
        const copyData = [];
        for (let key of showField) {
            const val = data[key];
            if (type === 0 && typeof val === "number") {
                copyData.push(val);
            }
            else if (type === 1 && typeof val === "string") {
                copyData.push(val);
            }
            else if (type === 2) {
                copyData.push(val);
            }
        }
        copy.push(copyData.join("\t"));
    }
    copyToClipboard(copy.join("\r\n"));
};
/**
 * 判断坐标是否划选范围内
 * @param range
 * @param rowIndex
 * @param columnIndex
 */
export const isRange = function (range, rowIndex, columnIndex) {
    if (!(range.startRowIndex !== undefined &&
        range.endRowIndex !== undefined &&
        range.startColumnIndex !== undefined &&
        range.endColumnIndex !== undefined)) {
        return false;
    }
    let isRowRange = false;
    let isColumnRange = false;
    if (rowIndex >= range.startRowIndex && rowIndex <= range.endRowIndex) {
        isRowRange = true;
    }
    if (columnIndex >= range.startColumnIndex &&
        columnIndex <= range.endColumnIndex) {
        isColumnRange = true;
    }
    return isRowRange && isColumnRange;
};
/**
 * 范围统计功能
 * @param rangeVals
 */
export const rangeNumberStatistics = function (rangeVals) {
    let sum = 0;
    let count = 0;
    for (let i = 0; i < rangeVals.length; i++) {
        const item = rangeVals[i];
        if (item.dataType === "number" || item.dataType === "money-number") {
            const val = !Number.isNaN(+item.val) ? +item.val : 0;
            sum = val + sum;
            count++;
        }
    }
    return {
        sum,
        count,
        avg: sum / count,
    };
};
/**
 * 根据原生获取选中行统计
 * @param rangeVals
 */
export const rangeNumberStatisticsBySelectData = function (selectData, showField) {
    let sum = 0;
    let count = 0;
    for (let i = 0; i < selectData.length; i++) {
        const data = selectData[i];
        for (let key of showField) {
            const val = data[key];
            if (typeof val === "number") {
                sum = val + sum;
                count++;
            }
        }
    }
    return {
        sum,
        count,
        avg: sum / count,
    };
};
